
* LOCAL MONOTONICITY
* ESTIMATE QUARTER SPEC IV ESTIMATES BY DECILES OF CONDITIONAL MARGINAL DISTRIBUTIONS OF EXPECTED INFLATION
* 1. ESTIMATE CONDITIONAL QRS OF EXPECTED INFLATION AND FOR EACH OBS PREDICT THE CONDITIONAL QUANTILES q(X)
* 2. ASSIGN EACH OBS TO A SPECIFIC DECILE BASED ON COMAPRISON BETWEEN OBSERVED VALUE OF EXP INFL AND PREDICTED CONDITIONAL QUANTILE
* 3. ESTIMATE FIRST STAGE AND IV FOR EACH QUARTER ON EACH DECILE

quietly{

use WorkingData, clear

cap drop dec
gen dec=.
gen q20=.
gen q40=.
gen q60=.
gen q80=.

local explvar=" dpre i.dim i.area i.setton5 "

* PREDICT CONDITIONAL QUANTILES 
forvalues t=$firsttrim/$lasttrim{
	forvalue i=0/1{
		forvalues q=20(20)80{
			qui qreg it12all $_explvar if treat==$_i & dp==$_t, q($_q)
			if _rc==0{
				predict tmp if e(sample), xb
				replace q$_q = tmp if treat==$_i & dp==$_t
				drop tmp
			}
		}
	}
}

* ASSIGN EACH OBS TO SPECIFIC QUANTILE 
local d=1
forvalues q=20(20)80{
	replace dec=$_d if it12all<=q$_q & dec==. & q$_q!=.
	local d = $_d+1
}
replace dec=5 if it12all>q80 & it12all!=. & q80!=.



local explvar=" dpre i.dim i.area i.setton5 "


* IV REGS ON LOCAL SAMPLES (DEC) FOR EACH DEP VAR

foreach y in dprez F1DPZ F2DPZ F4DPZ  F1LAD F2LAD F3LAD F4LAD{
	
	mat def xbdec=J(42, 6,.)
	mat def xsdec=J(42, 6,.)

	local r=1
	forvalues q=$firsttrim/$lasttrim{
		mat xbdec[$_r,1] = $_q
		mat xsdec[$_r,1] = $_q
		forvalues d=1/5{
			cap qui ivregress 2sls $_y $_explvar (it12all=treatment) if dp==$_q & dec==$_d, vce(robust)
			if _rc==0{
				local c=$_d+1
				mat xbdec[$_r,$_c]=_b[it12all]
				mat xsdec[$_r,$_c]=_se[it12all]
			}

		}
		
		local r = $_r + 1
	}
	
	local n="$_y"
	
	mat def `n'XB = xbdec
	mat def `n'XS = xsdec

}
	
* FIRST STAGE REGS ON LOCAL SAMPLES (DEC)	
mat def txfirst=J($ntrims,6,.)
local r = 1
forvalues q=$firsttrim/$lasttrim{
	mat txfirst[$_r,1]=$_q
	forvalues d=1/5{
		local c=$_d+1
		qui reg it12all treat $_explvar if dp==$_q & dec==$_d, r
		mat txfirst[$_r,$_c]=_b[treat]/_se[treat]
	}
	local r=$_r+1
}
		

* TURN MATRIX STORING IV AND FIRST STAGE LOCAL RESULTS IN DTA TO BE USED FOR FIG
		
local i = 1
foreach y in dprez F1DPZ F2DPZ F4DPZ  F1LAD F2LAD F3LAD F4LAD{
	
	drop _all
	local n="$_y"
	svmat `n'XB
	svmat `n'XS
	count if `n'XS1!=`n'XB1
	if r(N)==0{
		forvalues d=1/5{
			local c = $_d+1
			gen TX`n'$_d = `n'XB$_c/`n'XS$_c
		}
		gen dp=`n'XB1
		keep dp TX*
		reshape long TX`n', i(dp) j(dec)
		reshape long TX, i(dp dec) j(var, string)
		
		if $_i>1{
			append using TXLocalIV
		}
		save TXLocalIV, replace
	}

	if r(N)!=0{
		di "$_n"
	}


	local i = $_i + 1
	
}

drop _all
svmat txfirst	
rename txfirst1 dp
forvalues j=1/5{
	local c=$_j+1
	rename txfirst$_c txfirst$_j
}
reshape long txfirst, i(dp) j(dec)
merge 1:m dp dec using TXLocalIV
save TXLocalIV, replace
	
}


* ***************** GENERATE FIG 9 *******************************

quietly{
use TXLocalIV, clear	

gen v=.
replace v=1 if var=="F1LAD"
replace v=2 if var=="F2LAD"
replace v=3 if var=="F3LAD"
replace v=4 if var=="F4LAD"
replace v=5 if var=="dprez"
replace v=6 if var=="F1DPZ"
replace v=7 if var=="F2DPZ"
replace v=8 if var=="F4DPZ"

lab def lv 1 "observed at t+1" 2 "observed at t+2" 3 "observed at t+3" 4 "observed at t+4" 5 "expected 1 year at t" 6 "observed at t+1" 7 "observed at t+2" 8 "observed at t+4"
lab val v lv

rename TX IV_Tstat
rename tx FS_Tstat 


scatter IV_Tstat FS_Tstat, by(v, note("") graphregion(c(white)) row(2)) subti(,bc(white))  xsize(10) ysize(6) yti("") xti("First stage (t-statistic)") yla(,angle(0) labsi(*0.8))  xli(-1.96 1.96) yli(-1.96 1.96) msy(oh) xla(-25(5)25, angle(90) labsi(*0.8)) yla(-3(1)3) 

gr_edit .plotregion1.l2title[1]._set_orientation vertical
gr_edit .plotregion1.l2title[1].text = {}
gr_edit .plotregion1.l2title[1].text.Arrpush Labor demand (t-stat)
gr_edit .plotregion1.l2title[5].text = {}
gr_edit .plotregion1.l2title[5].text.Arrpush Annual price change (t-stat)

graph export Fig9.eps, replace

}

* ***************** GENERATE FIG 9: END *******************************
* =======================================
* PRINT TO LOG DATA FOR FIG 9
* SEE ABOVE FOR CONSTRUCTION OF FIG

lab var IV_Tstat "Tstat of IV estimate of effect of expected inflation on depvar"
lab var FS_Tstat "Tstat of first stage effect of info assignment"

sort v

* ***************************
* **** DATA FOR FIG 9 *******
* ***************************
list var v IV_Tstat FS_Tstat, linesize(255) noobs header

